<template>
	<!-- 商品列表 -->
	<view>
		<view v-if="tempArr.length" :style="[boxStyle]">
			<view class="group-title-box">
				<view class="group-content-left" :style="{ 'background-image': `url(${groupBgSrc})` }">
					<image :src="groupSpotSrc" mode=""></image>
					<span>正在开团</span>
					<image :src="groupSpotSrc" mode=""></image>
				</view>
				<view class="group-content-right">即将上架</view>
			</view>
			<!-- 两列 -->
			<block>
				<view class="listC">
					<view class="item" :style="[contentBgColor]" v-for="(item, index) in tempArr" :key="index"
						@click="goDetail(item)">
						<view class="pictrue">
							<easy-loadimage :image-src="item.image" :radius="dataConfig.contentStyle.val"
								:mode="'aspectFill'">
							</easy-loadimage>
							<view v-if="item.activityStyle" :style="{ backgroundImage: `url(${item.activityStyle})` }"
								class="border-picture"></view>
						</view>
						<view class="text-info">
							<view class="title tn-text-ellipsis" :style="[titleColor]">
								<text v-if="item.productTags && item.productTags.locationLeftTitle.length"
									class="font-bg-red mr10 merType bg-color">{{item.productTags.locationLeftTitle[0].tagName}}</text>
								<span>{{ item.name }}</span>
							</view>
							<view class="acea-row row-middle price" :style="[priceColor]">
								<view>
									<text>￥</text>
									{{ item.price }}
									<text class="original-price">￥{{item.otPrice}}</text>
								</view>
								<view class="go-buy-btn">
									<span>26人都在买</span>
									<view class="buy-btn">立即购买</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</block>
			<view class='loadingicon acea-row row-center-wrapper' :hidden='loading==false'>
				<text class='loading iconfont icon-jiazai'></text>
			</view>
			<!-- <view class="mores-txt" v-if="goodScroll">
				<text>我是有底线的</text>
			</view> -->
		</view>
	</view>
</template>

<script>
	import {
		productList,
		productByidsApi
	} from '@/config/api.js';
	import easyLoadimage from '@/components/easy-loadimage.vue';
	import store from '../store';
	export default {
		name: 'productGrid',
		props: {
			dataConfig: {
				type: Object,
				default: () => {}
			},
		},
		components: {
			easyLoadimage
		},
		data() {
			return {
				tempArr: [],
				numConfig: this.dataConfig.numConfig.val, //展示多少条
				type: this.dataConfig.tabConfig.tabVal || 0, //商品类型 0指定商品，1指定品牌，2指定分类，3指定商户
				selectId: this.dataConfig.selectConfig ? this.dataConfig.selectConfig.activeValue : [], //分类
				brandId: this.dataConfig.activeValueBrand ? this.dataConfig.activeValueBrand.activeValue : [], //品牌
				productIds: this.dataConfig.goodsList.ids || [],
				merId: this.dataConfig.activeValueMer ? this.dataConfig.activeValueMer.activeValue : [], //商户
				params: { //精品推荐分页
					page: 1,
					limit: 10,
					cid: '',
					brandId: '',
					merId: '',
					priceOrder: '',
					salesOrder: ''
				},
				goodScroll: false,
				loading: false,
				groupBgSrc: store.state.imgUrl + '/crmebimage/mer_plat_admin/group-bg.svg',
				groupSpotSrc: store.state.imgUrl + '/crmebimage/mer_plat_admin/group-spot.svg',
			};
		},
		computed: {
			//商品名称颜色
			titleColor() {
				return {
					'color': this.dataConfig.titleColor.color[0].item,
				}
			},
			//最外层盒子的样式
			boxStyle() {
				return {
					margin: this.dataConfig.mbConfig.val * 2 + 'rpx' + ' ' + this.dataConfig.lrConfig.val * 2 + 'rpx' +
						' ' + 0,
					padding: this.dataConfig.upConfig.val * 2 + 'rpx' + ' ' + 0 + ' ' + this.dataConfig.downConfig.val *
						2 + 'rpx'
				}
			},
			//文章图片的圆角和高度
			imgStyle() {
				return {
					'border-radius': this.dataConfig.contentStyle.val * 2 + 'rpx',
				}
			},
			//价格颜色
			priceColor() {
				return {
					'color': this.dataConfig.priceColor.color[0].item,
				}
			},
			//排序，0综合，1销量，2价格
			goodsSort() {
				return this.dataConfig.goodsSort.tabVal
			},
			//内容圆角
			contentStyle() {
				return {
					'border-radius': this.dataConfig.contentStyle.val ? this.dataConfig.contentStyle.val + 'px' : '0',
				};
			},
			// 背景圆角
			contentBgColor() {
				return {
					'background': `linear-gradient(${this.dataConfig.bgColor.color[0].item}, ${this.dataConfig.bgColor.color[1].item})`,
					'borderRadius': this.dataConfig.bgStyle.val * 2 + 'rpx',
					'margin-bottom': this.dataConfig.contentConfig.val * 2 + 'rpx',
				}
			}
		},
		mounted() {
			this.params.page = 1;
			this.goodScroll = false;
			this.tempArr = [];
			//类型为0时，直接加载选中的商品，不为0时根据条件加载商品列表
			if (this.type > 0) {
				this.productslist();
			} else {
				this.getProList();
			}
		},
		//uniapp小程序用deep重写组件样式不生效
		options: {
			styleIsolation: 'shared'
		},
		methods: {
			//根据商品id集合查询对应商品
			getProductByids(data) {
				uni.showLoading({
					title: '加载中...'
				});
				let ids = data.map((item) => item.id).join(',');
				productByidsApi(ids).then((res) => {
						this.tempArr = res.data;
						uni.hideLoading();
					})
					.catch(res => {
						uni.hideLoading();
					});
			},
			getProList() {
				this.getProductByids(this.dataConfig.goodsList.list);
			},
			productslist() {
				if (this.goodScroll) return;
				this.loading = true
				this.params.limit = this.numConfig;
				this.params.cid = this.selectId.join(',');
				this.params.brandId = this.brandId.join(',');
				this.params.merId = this.merId.join(',');
				if (this.goodsSort === 0) {
					this.params.priceOrder = '';
					this.params.salesOrder = '';
				} else if (this.goodsSort === 1) {
					this.params.priceOrder = '';
					this.params.salesOrder = 'desc';
				} else {
					this.params.priceOrder = 'desc';
					this.params.salesOrder = '';
				}

				productList(this.params).then(res => {
					this.$set(this.params, 'page', this.params.page + 1);
					this.goodScroll = this.params.page > res.data.totalPage;
					this.tempArr = this.tempArr.concat(res.data.list || []);
					this.loading = false
				});
			},
			goDetail(item) {
				this.$emit('detail', item);
			}
		}
	};
</script>

<style lang="scss" scoped>
	.mores-txt {
		text-align: center;
	}



	.listC {
		width: 100%;
		overflow: hidden;

		/deep/.origin-img,
		/deep/.easy-loadimage {
			border-bottom-left-radius: 0 !important;
			border-bottom-right-radius: 0 !important;
		}

		.item {
			background-color: #fff;
			overflow: hidden;

			.pictrue {
				width: 100%;
				height: 345rpx;
				overflow: hidden;
				position: relative;

				image {
					width: 100%;
					height: 100%;
				}
			}
		}
	}

	.text-info {
		padding: 16rpx 24rpx;

		.title {
			width: 100%;
			line-height: 40rpx;
			color: #1a1a1a;
			font-size: 14px;
			font-weight: bold;
		}

		.price {
			font-size: 32rpx;
			font-weight: bold;
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-top: 14rpx;
			color: #000000;

			text {
				padding-bottom: 4rpx;
				font-size: 24rpx;
			}

			.original-price {
				font-weight: 400;
				font-size: 20rpx;
				color: #808080;
				text-decoration: line-through;
				margin-left: 10rpx;
			}

			.go-buy-btn {
				display: flex;
				align-items: center;
				font-weight: 400;

				.buy-btn {
					width: 70px;
					height: 26px;
					background: linear-gradient( 131deg, #FE0000 0%, #FE6715 100%);
					border-radius: 6rpx;
					font-weight: 800;
					font-size: 12px;
					color: #ffffff;
					line-height: 52rpx;
					text-align: center;
					margin-left: 16rpx;
				}

				span {
					font-size: 10px;
					color: #808080;
				}
			}
		}
	}



	.group-title-box {
		display: flex;
		align-items: flex-start;
		justify-content: center;
		margin-bottom: 28rpx;

		.group-content-left {
			width: 50%;
			height: 96rpx;
			background-size: 100% 100%;
			background-repeat: no-repeat;
			padding-bottom: 16rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			font-family: PingFang-SC, PingFang-SC;
			font-weight: 800;
			font-size: 32rpx;
			color: #ffffff;

			span {
				margin: 0 6px;
			}

			image {
				width: 8px;
				height: 7px;
			}
		}

		.group-content-right {
			width: 50%;
			height: 80rpx;
			background: #FEF1F1;
			border-radius: 6rpx;
			font-weight: 400;
			font-size: 32rpx;
			color: #FE0000;
			text-align: center;
			line-height: 80rpx;
		}
	}
</style>